package com.ruicar.afs.cloud.account.common.mq.receiver.processor;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruicar.afs.cloud.account.common.service.VoucherFlowInfoService;
import com.ruicar.afs.cloud.batch.entity.AfsBatchInfo;
import com.ruicar.afs.cloud.batch.service.AfsBatchInfoService;
import com.ruicar.afs.cloud.common.modules.dto.mq.voucher.VoucherFlowInfoDto;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * <p>Description:凭证流水接收处理 </p>
 * @author infinity
 * @version 1.0
 * @date 2020/8/12 12:04
 */
@Slf4j
@Component
@AllArgsConstructor
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit",name="enable")
public class VoucherFlowProcess implements AfsMqBizProcessor<VoucherFlowInfoDto> {
    private final VoucherFlowInfoService voucherFlowInfoService;
    private final AfsBatchInfoService afsBatchInfoService;
    @Override
    public boolean processMessage(VoucherFlowInfoDto dto) throws Exception {
        AfsBatchInfo afsBatchInfo = afsBatchInfoService.getSystemBatchInfo("icos"); //获取批量日
        Date date = DateUtil.parse(afsBatchInfo.getBatchDate(), DatePattern.PURE_DATE_PATTERN);
        dto.setKeepAccountDate( date );
        return voucherFlowInfoService.checkAndSaveFlow(dto);
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.AFS_VOUCHER_FLOW_PUSH;
    }
}
